/*

Copyright 2007 Renaun Erickson (http://renaun.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ignore
*/
package asfslib.services
{
import asfslib.FamilySearchService;
import asfslib.events.FamilySearchErrorEvent;

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
	
[Event(name="error", type="asfslib.events.FamilySearchErrorEvent")]	

/**
 * 	The base service class for all fsapi services
 */
public class BaseService extends EventDispatcher
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

	/**
	 * 	Constructor
	 */
	public function BaseService(service:FamilySearchService)
	{
		this.service = service;

        if (!serviceLoader)
        {
        	serviceLoader = new URLLoader();
				
        	serviceLoader.addEventListener(Event.COMPLETE, 					completeHandler);
        	serviceLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, 	httpStatusHandler);
        	serviceLoader.addEventListener(IOErrorEvent.IO_ERROR, 			errorHandler);
        }
		
	}	

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------
	
	protected var service:FamilySearchService;
	public var endpoint:String;
	
	protected var serviceLoader:URLLoader;

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

	/**
	 * 	Handler called when the loader completes the event
	 */
	protected function completeHandler(event:Event):void
	{
		//dispatchEvent(event);
	}

	/**
	 * 	Handler called when the loader receives a HTTPStatus event
	 */
	protected function httpStatusHandler(event:HTTPStatusEvent):void
	{
		dispatchEvent(event);
	}

	/**
	 * 	Handler called when the loader receives a IOErrorEvent event
	 */
	protected function errorHandler(event:IOErrorEvent):void
	{
		//var serviceEvent:FamilySearchServiceEvent = new FamilySearchServiceEvent(FamilySearchServiceEvent.SEARCH,false,false,null,"Service Error");
		dispatchEvent(event);
	}

	/**
	 * 	Helper class to check for fsapi errors and send off the error event
	 */
	protected function checkFSAPIErrors(resultXML:XML):Boolean
	{
		var fsapiNS:Namespace = resultXML.namespace("fsapi-" + FamilySearchService.AUTHORITIES_VERSION_STRING);
		var hasErrors:Boolean = ((resultXML.fsapiNS::errors as XMLList).length() > 0);
		if (hasErrors)
		{
  			var code:int = int(resultXML.fsapiNS::errors.fsapiNS::error.@code);
  			var message:String = resultXML.fsapiNS::errors.fsapiNS::error.fsapiNS::message;
  			dispatchEvent(new FamilySearchErrorEvent(FamilySearchErrorEvent.ERROR, false, false, message, code));
  		}		
  		return hasErrors;
	}

}
}